﻿using Controller.i18n.entity;
using Controller.i18n.plc.crane;
using HBase;
using hCommand;
using hconnect;
using Net.http;
using System;
using System.Collections.Generic;

namespace Controller {
    //输送线
    public abstract class CraneSynchro : AbsSynchro {
  
        protected CraneConnect LmModbusConnect = null;
        protected IWCSServer taskServer = WMSDalFactray.getDal<IWCSServer>();
        protected override bool onlyOneTimeAsync() {
            LmModbusConnect = TaskPool.get<CraneConnect>(getCNo());
         
            return true;
        }

        protected override AbsStatus parse(AbsStatus old) {
            CraneStatus cs = null;
            if (old == null) {
                cs = new CraneStatus(getCNo());
            } else {
                cs = new CraneStatus((CraneStatus)old);
            }
            byte[] statebyte = null;
            if (cs.id == 91) {

                statebyte = LmModbusConnect.reader(501, 0, 30);
            } else {
                statebyte = LmModbusConnect.reader(501, 0, 32);
            }


            if (statebyte == null) {
                return null;
            }
            cs.parseStatus(statebyte);
            //if (cs.error == 0)
            //{
            byte[] errors = LmModbusConnect.reader(9, 0, 16);
            cs.parseError(errors);
            //}

            return cs;
        }

        public override MyDict<int, TaskStatusPersistence> getTaskNo(AbsStatus status) {
            CraneStatus cs = (CraneStatus)status;
            string error = cs.getError();
            MyDict<int, TaskStatusPersistence> dict = new MyDict<int, TaskStatusPersistence>();
            TaskCrane tc = BaseTask.getTaskByTaskNo<TaskCrane>(cs.finishTaskNo);
            if (tc == null) {
                if (cs.finishTaskNo > 0) {
                    CraneConnect craneConnect = TaskPool.get<CraneConnect>(cs.id);
                    craneConnect.writeShort(500, 24, 2);
                    fileAll(0, "删除垛机任务" + cs.finishTaskNo);
                }
                return dict;
            }
            dict.Add(cs.finishTaskNo, new TaskStatusPersistence(cs.finishTaskNo, 0, tc, (CoreTaskParamStatus)cs.finishFlag, error));
            return dict;
        }


    }
}
